package com.newcoder.controller.filter;

import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class CustomizeFilter implements Filter {

    private static final Logger logger = LoggerFactory.getLogger(CustomizeFilter.class);


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // 初始化操作（可选）
        //System.out.println("CustomizeFilter initialized.");
        logger.info("CustomizeFilter initialized.");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        // 在这里添加你想要执行的逻辑，例如日志记录、权限检查等
        // 在请求处理之前的操作
        //System.out.println("Before processing the request");
        // 记录请求信息
        logger.info("Request intercepted by CustomizeFilter: " + ((HttpServletRequest) request).getRequestURI());

        // 继续处理请求(继续链中的下一个过滤器或目标资源)
        chain.doFilter(request, response);

        // 在请求处理之后的操作,可以在这里处理响应相关的事情
        //System.out.println("After processing the request");
    }

    @Override
    public void destroy() {
        // 当过滤器被销毁时调用（可选）
        //System.out.println("CustomizeFilter destroyed.");
        logger.info("CustomizeFilter destroyed.");
    }
}